﻿#include "jsonconfig.h"

#include "sdk/QsLog/include/QsLog.h"

#include <QDebug>
#include <QApplication>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

JsonConfig::JsonConfig()
{
    // 读取JSON配置文件
    QString filename = QString::fromUtf8("%1/%2.json").arg(qApp->applicationDirPath(), qApp->applicationName());
    readConfig(filename);
}

QString JsonConfig::version() const
{
    return m_version;
}

QString JsonConfig::deviceId() const
{
    return m_deviceId;
}

QString JsonConfig::hardwareVersionCmd() const
{
    return m_hardwareVersionCmd;
}

QString JsonConfig::softwareVersionCmd() const
{
    return m_softwareVersionCmd;
}

QString JsonConfig::realtimePositionCmd() const
{
    return m_realtimePositionCmd;
}

QString JsonConfig::zeroCmd() const
{
    return m_zeroCmd;
}

QString JsonConfig::centerCmd() const
{
    return m_centerCmd;
}

int JsonConfig::centerPosition() const
{
    return m_centerPosition;
}

int JsonConfig::gpioReset() const
{
    return m_gpioReset;
}

int JsonConfig::gpioResetLatch() const
{
    return m_gpioResetLatch;
}

QList<FixedChannel> JsonConfig::fixedChannelList() const
{
    return m_fixedChannelList;
}

double JsonConfig::plotXSize() const
{
    return m_plotXSize;
}

double JsonConfig::plotYMin() const
{
    return m_plotYMin;
}

double JsonConfig::plotYMax() const
{
    return m_plotYMax;
}

GraphProperty JsonConfig::graphProperty(const QString &id) const
{
    return m_graphPropertyMap.value(id);
}

QMap<QString, VideoPlayback> JsonConfig::playbackMap(const QString &type) const
{
    if (QString::compare(type, "one") == 0) {
        return m_onePlaybackMap;
    } else if (QString::compare(type, "two") == 0) {
        return m_twoPlaybackMap;
    } else if (QString::compare(type, "three") == 0) {
        return m_threePlaybackMap;
    } else if (QString::compare(type, "four") == 0) {
        return m_fourPlaybackMap;
    } else {
        return QMap<QString, VideoPlayback>();
    }
}

void JsonConfig::readConfig(const QString &filename)
{
    QFile file(filename);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QLOG_ERROR() << QString("****fail to open json file: %1, %2, %3").arg(__FILE__).arg(__LINE__).arg(__FUNCTION__);
        return;
    }
    QByteArray content = file.readAll();
    file.close();

    QJsonParseError error;
    QJsonDocument document(QJsonDocument::fromJson(content, &error));
    if (error.error != QJsonParseError::NoError) {
        QLOG_ERROR() << QString("****json parse error: %1").arg(error.errorString());
        return;
    }

    // JSON文件内容, 从root节点开始
    QJsonObject root = document.object();

    // 版本, 设备编号
    m_version = root["version"].toString();
    m_deviceId = root["deviceid"].toString();

    // 硬件版本, 软件版本, 实时位置, 回原点指令, 运行到中心的绝对位置指令
    QJsonObject cmdOjbect = root["cmd"].toObject();
    m_hardwareVersionCmd = cmdOjbect.value("hardwareversion").toString();
    m_softwareVersionCmd = cmdOjbect.value("softwareversion").toString();
    m_realtimePositionCmd = cmdOjbect.value("realtimeposition").toString();
    m_zeroCmd = cmdOjbect.value("zero").toString();
    m_centerCmd = cmdOjbect.value("center").toString();
    // 中心的绝对位置
    QJsonObject servoObject = root["servo"].toObject();
    m_centerPosition = servoObject.value("center").toInt();

    // 复位和复位封锁
    QJsonObject gpioObject = root["gpio"].toObject();
    m_gpioReset = gpioObject.value("reset").toInt();
    m_gpioResetLatch = gpioObject.value("resetlatch").toInt();

    // fixedchannels节点
    QJsonArray fixedChannels = root["fixedchannels"].toArray();
    for (int i = 0; i < fixedChannels.size(); i++) {
        QJsonObject fixedChannelObject = fixedChannels.at(i).toObject();
        FixedChannel fixedChannel;
        fixedChannel.id = fixedChannelObject.value("id").toString();
        fixedChannel.name = fixedChannelObject.value("name").toString();
        m_fixedChannelList << fixedChannel;
    }

    // plot节点
    QJsonObject plotOjbect = root["plot"].toObject();
    m_plotXSize = plotOjbect.value("xsize").toDouble();
    m_plotYMin = plotOjbect.value("ymin").toDouble();
    m_plotYMax = plotOjbect.value("ymax").toDouble();
    QJsonArray graphs = plotOjbect.value("graphs").toArray();
    for (int i = 0; i < graphs.size(); i++) {
        GraphProperty graphProperty;
        QJsonObject graphObject = graphs.at(i).toObject();
        graphProperty.id = graphObject.value("id").toString();
        graphProperty.name = graphObject.value("name").toString();
        QJsonObject colorObject = graphObject.value("color").toObject();
        QColor color(colorObject.value("r").toInt(), colorObject.value("g").toInt(), colorObject.value("b").toInt());
        graphProperty.color = color;
        m_graphPropertyMap.insert(graphProperty.id, graphProperty);
    }

    // video节点
    QJsonObject videoObject = root["video"].toObject();
    QJsonObject oneObject = videoObject.value("one").toObject();
    QStringList onePlaybackList = oneObject.keys();
    for (QString &key : onePlaybackList) {
        QJsonObject playbackObject = oneObject.value(key).toObject();
        VideoPlayback videoPlayback;
        videoPlayback.startTime = playbackObject.value("starttime").toInt();
        videoPlayback.duration = playbackObject.value("duration").toInt();
        m_onePlaybackMap.insert(key, videoPlayback);
    }
    QJsonObject twoObject = videoObject.value("two").toObject();
    QStringList twoPlaybackList = oneObject.keys();
    for (QString &key : twoPlaybackList) {
        QJsonObject playbackObject = twoObject.value(key).toObject();
        VideoPlayback videoPlayback;
        videoPlayback.startTime = playbackObject.value("starttime").toInt();
        videoPlayback.duration = playbackObject.value("duration").toInt();
        m_twoPlaybackMap.insert(key, videoPlayback);
    }
    QJsonObject threeObject = videoObject.value("three").toObject();
    QStringList threePlaybackList = oneObject.keys();
    for (QString &key : threePlaybackList) {
        QJsonObject playbackObject = threeObject.value(key).toObject();
        VideoPlayback videoPlayback;
        videoPlayback.startTime = playbackObject.value("starttime").toInt();
        videoPlayback.duration = playbackObject.value("duration").toInt();
        m_threePlaybackMap.insert(key, videoPlayback);
    }
    QJsonObject fourObject = videoObject.value("four").toObject();
    QStringList fourPlaybackList = oneObject.keys();
    for (QString &key : fourPlaybackList) {
        QJsonObject playbackObject = fourObject.value(key).toObject();
        VideoPlayback videoPlayback;
        videoPlayback.startTime = playbackObject.value("starttime").toInt();
        videoPlayback.duration = playbackObject.value("duration").toInt();
        m_fourPlaybackMap.insert(key, videoPlayback);
    }

}




